const WXAPI = require('apifm-wxapi')
const CONFIG = require('../../config.js')
//获取应用实例
var app = getApp();
Page({
  data: {
    sjc: Date.parse(new Date()),
    banners: [],
    jindex: 0,
    all: 0,
    touchStartY: 0, // 用于记录触摸开始的 Y 坐标
    isSwiping: false // 防止多次触发

  },
  onShareAppMessage() {},
  
  onShareTimeline() {},
  // 记录触摸起点
  handleTouchStart(e) {
    this.setData({
      touchStartY: e.touches[0].clientY,
      isSwiping: true
    });
  },

  // 处理滑动
  handleTouchMove(e) {
    const { touchStartY, isSwiping, jindex, all } = this.data;
    const touchMoveY = e.touches[0].clientY;
    const diff = touchMoveY - touchStartY;

    if (Math.abs(diff) > 30 && isSwiping) { // 判断滑动距离是否足够
      this.setData({ isSwiping: false }); // 锁定，防止重复触发

      if (diff < 0) {
        // 向上滑动
        if (jindex < all) {
          this.setData({ jindex: jindex + 1 });
        } else {
          this.setData({ jindex: 0 }); // 循环回到第一个
        }
      } else {
        // 向下滑动
        if (jindex > 0) {
          this.setData({ jindex: jindex - 1 });
        } else {
          this.setData({ jindex: all }); // 循环到最后一个
        }
      }
    }
  },


  up() {
    // console.log("up", this.data.jindex, this.data.all);
    if (this.data.jindex == 0) {
      // console.log("00");
      this.setData({
        jindex: this.data.all
      })
    }
    else if (this.data.jindex > 0) {
      // console.log("--");
      this.setData({
        jindex: this.data.jindex - 1
      })
    }
    console.log("jindex", this.data.jindex);
  },
  down() {
    // console.log("down", this.data.jindex, this.data.all);
    if (this.data.jindex == this.data.all) {
      // console.log("00");
      this.setData({
        jindex: 0
      })
    }
    else if (this.data.jindex < this.data.all) {
      // console.log("++");
      this.setData({
        jindex: this.data.jindex + 1
      })
    }

    console.log("jindex", this.data.jindex);
  },
  onShow: function () {
    var that = this;
  },
  async onLoad() {

    const _this = this
    var xx = await WXAPI.cmsArticles({
      categoryId: 195840,
      page: 1,
      pageSize: 20
    })
    console.log("xx", xx.data.result);

    let temp = xx.data.result;
    //在循环调用详情let res = await WXAPI.cmsArticleDetailV2(a);
    for (let i = 0; i < temp.length; i++) {
      let res = await WXAPI.cmsArticleDetailV2(temp[i].id);
      if (res.code == 0) {
        temp[i].dm_img = (res.data.extJson.dm_img).split(";");
        temp[i].dm_wz = (res.data.extJson.dm_wz).split(",");
        if (res.data.extJson.dm_zdy) {
          temp[i].dm_zdy = (res.data.extJson.dm_zdy).split(",");
        }
      }
    }


    _this.setData({
      banners: temp,
      all: temp.length - 1
    });

    console.log("temp", temp, _this.data.all);


  },

  bt(e) {
     wx.vibrateShort(); 
    let temp = e.currentTarget.dataset.id;
    // 判断是纯数字的跳转到详情页，否则列表页
    if (temp.match(/^[0-9]*$/)) {
      wx.navigateTo({
        url: `/pages/xiangqing/index?id=${temp}`,
      })
    } else {
      wx.navigateTo({
        url: `/pages/taglist/index?id=${temp}`,
      });
    }
  },

  bts(e) {
     wx.vibrateShort(); 
    let temp = e.currentTarget.dataset.id;
    if (temp == 1) {
      wx.navigateTo({
        url: '/pages/start/start'
      })
    }
    // 发现
    if (temp == 2) {
      wx.navigateTo({
        url: '/pages/index_more/index?id=2'
      })
    }
    // 宇宙
    if (temp == 3) {
      wx.navigateTo({
        url: '/pages/index_more/index?id=3'
      })
    }
    // 生态
    if (temp == 4) {
      wx.navigateTo({
        url: '/pages/index_more/index?id=4'
      })
    }
    // 星球
    if (temp == 5) {
        wx.navigateTo({
        url: '/pages/index_more/index?id=5'
      })
    }
    // 好物
    if (temp == 6) {
      wx.switchTab({
        url: '/pages/haowu/index'
      })
    }
  },





  goToIndex: function (e) {
    let shopMod = wx.getStorageSync('shopMod')
    if (!shopMod) {
      shopMod = 0
    }
    if (app.globalData.isConnected) {
      wx.setStorage({
        key: 'app_show_pic_version',
        data: CONFIG.version
      })
      if (shopMod == 1) {
        wx.redirectTo({
          url: '/pages/shop/select',
        });
      } else {
        wx.switchTab({
          url: '/pages/index/index',
        });
      }
    } else {
      wx.showToast({
        title: '当前无网络',
        icon: 'none',
      })
    }
  }
});